package com.oracle.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oracle.compents.ApiResponse;
import com.oracle.pojo.Major;
import com.oracle.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/major")
public class MajorController {

    @Autowired
    private MajorService majorService;


    @PostMapping("/search")
    public ApiResponse search (@RequestBody Major major, Integer pageNum , Integer pageSize){
        IPage<Major> majorIPage = majorService.findMajor(major, pageNum, pageSize);
        if (ObjectUtils.isEmpty(majorIPage)){
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("专业模块，查询不到该专业");
            return failed;
        }else{
            return ApiResponse.success(majorIPage);
        }
    }

    @PostMapping("/insert")
    public ApiResponse insert (@RequestBody Major major){
        int i = majorService.insertMajor(major);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

    @GetMapping("/findOne")
    public ApiResponse findOne (Integer id){
        Major oneMajor = majorService.findOneMajor(id);
        if(ObjectUtils.isEmpty(oneMajor)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(oneMajor);
        }
    }

    @GetMapping("/findAll")
    public ApiResponse findAll(){
        List<Major> allMajor = majorService.findAllMajor();
        if(ObjectUtils.isEmpty(allMajor)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(allMajor);
        }
    }

    @PostMapping("/updataMajor")
    public ApiResponse updataMajor (@RequestBody Major major){
        int i = majorService.updateMajor(major);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

    @PostMapping("/delete")
    public ApiResponse delete (@RequestBody Major major){
        Integer majorId = major.getMajorId();
        int i = majorService.deleteMajor(majorId);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

}
